<?php
//接任务
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return['set']=$this->plugin_setting('basic');
if($_RQ['op']=='main'){
	$taskid=$_RQ['taskid'];
	$mid=$_SESSION['uid'];
	//print_r($mid.'fgff');exit;
	$task=pdo_get('jy_renwu_task_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$taskid));
	
	$member=pdo_get('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$mid));
	if (empty($task)){
		exi('任务不存在','error');
	}
	$canPick =checkCanPick($task, $mid);
	if ($canPick['status']==-1) {
		$return['canPick']=$canPick;
		exi($return);
	}
	$stoptime =stoptime($task);
	//print_r($stoptime.'事件');exit;
	$taskArr = array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'mid' => $mid, 'nickname' => $member['nickname'], 'picktime' => date('Y-m-d H:i:s'), 'task_demand' =>$task['demand'], 'taskimage' => $task['image'], 'reward_data' => $task['reward'], 'followreward_data' => $task['followreward'], 'design_data' => $task['design_data'], 'design_bg' => $task['design_bg'], 'stoptime' => $stoptime, 'require_goods' => $task['requiregoods'], 'member_group' => $task['member_group'], 'auto_pick' => $task['auto_pick']);
	if ($task['type'] == 'poster' && 0 < $task['level2']) {
		$taskArr['level1'] = $task['demand'];
		$taskArr['reward_data1'] = $task['reward'];
		$taskArr['level2'] = $task['level2'];
		$taskArr['reward_data2'] = $task['reward2'];

		if (0 < $task['level3']) {
			$taskArr['reward_data'] = $task['reward3'];
		}
		else {
			$taskArr['reward_data'] = $task['reward2'];
		}
	}
	$table = 'jy_renwu_task_record';
	//print_r($taskArr);exit;
	$recordid = taskSave($table, $taskArr, false);
	//print_r(!is_array($taskArr));exit;
	if (!$recordid) {
		return array('status'=>'-1','num'=>'1','message'=>'任务接取失败了');
	}

	$reward = json_decode($task['reward'], true);
	$level = 0;

	if ($task['type'] === 'poster') {
		if (0 < $task['level3'] || 0 < $task['level2']) {
			$level = 1;
		}
	}
	if (is_array($reward)) {
		foreach ($reward as $ke => $re) {
			if (is_array($re)) {
				foreach ($re as $r) {
					while (0 < $r['num']) {
						pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $r['id'], 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $ke == 'coupon' ? $r['couponname'] : $r['title'], 'price' => $task['type'] == 'coupon' ? 0 : $r['price'], 'level' => $level));
						--$r['num'];
					}
				}
			}
			else {
				if ($re) {
					if ($ke == 'credit') {
						$reward_title = '积分';
					}
					else if ($ke == 'balance') {
						$reward_title = '元余额';
					}
					else {
						if ($ke == 'redpacket') {
							$reward_title = '元微信红包';
						}
					}

					pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $re, 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $re . $reward_title, 'level' => $level));
				}
			}
		}
	}
	$level = 2;

	if ($task['type'] === 'poster') {
		if ($task['level3'] == 0) {
			$level = 0;
		}
	}

	$reward2 = json_decode($task['reward2'], true);
	if (is_array($reward2)) {
		foreach ($reward2 as $ke => $re) {
			if (is_array($re)) {
				foreach ($re as $r) {
					while (0 < $r['num']) {
						pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $r['id'], 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $ke == 'coupon' ? $r['couponname'] : $r['title'], 'price' => $task['type'] == 'coupon' ? 0 : $r['price'], 'level' => $level));
						--$r['num'];
					}
				}
			}
			else {
				if ($re) {
					if ($ke == 'credit') {
						$reward_title = '积分';
					}
					else if ($ke == 'balance') {
						$reward_title = '元余额';
					}
					else {
						if ($ke == 'redpacket') {
							$reward_title = '元微信红包';
						}
					}

					pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $re, 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $re . $reward_title, 'level' => $level));
				}
			}
		}
	}

	$reward3 = json_decode($task['reward3'], true);

	if (is_array($reward3)) {
		foreach ($reward3 as $ke => $re) {
			if (is_array($re)) {
				foreach ($re as $r) {
					while (0 < $r['num']) {
						pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $r['id'], 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $ke == 'coupon' ? $r['couponname'] : $r['title'], 'price' => $task['type'] == 'coupon' ? 0 : $r['price'], 'level' => 0));
						--$r['num'];
					}
				}
			}
			else {
				if ($re) {
					if ($ke == 'credit1') {
						$reward_title = '积分';
					}
					else if ($ke == 'credit2') {
						$reward_title = '元余额';
					}
					else {
						if ($ke == 'red') {
							$reward_title = '元微信红包';
						}
					}

					pdo_insert('jy_renwu_task_reward', array('uniacid' => $_SESSION['uniacid'], 'taskid' => $task['id'], 'tasktitle' => $task['title'], 'tasktype' => $task['type'], 'taskowner' => $mid, 'ownernickname' => $member['nickname'], 'recordid' => $recordid, 'reward_type' => $ke, 'reward_data' => $re, 'nickname' => $member['nickname'], 'mid' => $member['id'], 'headimg' => $member['avatar'], 'reward_title' => $re . $reward_title, 'level' => 0));
				}
			}
		}
	}

	$taskArr['id'] = $recordid;
	$taskArr['stoptime'] = $stoptime;
	/*if ($task['type'] == 'poster') {
		//$this->posterPickMessage($openid, $taskArr);
		$taskArr['design_data'] = $task['design_data'];
		$taskArr['design_bg'] = $task['design_bg'];
		if ($_W['ispost'] && !empty($_POST['openid'])) {
			$poster = $this->create_poster(array('id' => $taskArr['id'], 'design_data' => $taskArr['design_data'], 'design_bg' => $taskArr['design_bg'], 'stoptime' => $taskArr['stoptime']));
			$this->send2wechat($recordid, $openid);
		}
	}
	else {
		$this->taskPickMessage($openid, $taskArr);
	}
	*/
	$return['recordid']=$recordid;
	exi('任务接取成功！','','/mine/');
}
